<?php
namespace cosocket\system\server\abstracts;

use cosocket\application\Handler;

abstract class AbstractWebSocket
{
    protected $host;
    protected $port;
    protected $config;
    protected $ws;
    protected $handler;

    public function __construct($host = '0.0.0.0', $port = 5678, array $config = [])
    {
        $this->host = $host;
        $this->port = $port;
        $this->config = $config;
        $this->handler = new Handler();
    }

    abstract public function run();
    abstract public function initializeServer();
    abstract public function start();

    public function processFrame($frame, $ws, $request)
    {
        if (empty($frame) || empty($frame->data)) {
            return;
        }
        $this->handler->processFrame($frame, $ws, $request);
    }

    public function handleClose($frame, $ws, $reason = '')
    {
        $this->handler->handleClose($frame, $ws, $reason);
    }

    public function handleMessage($frame, $ws, $request)
    {
        $this->handler->handleMessage($frame, $ws, $request);
    }

}